var express = require("express");
var router = express();
// var request = require("request");
// var jwt = require("jsonwebtoken");
var db = require("../config/mysql");
var vertoken = require("../config/token");
// var { appid, appSecret } = require("../config/wx");

/**
 * @api {get} /api/front/songs 获取全部歌曲,通过歌曲名称搜索
 * @apiDescription 获取全部歌曲,通过歌曲名称搜索
 * @apiName songs
 * @apiGroup Front
 *
 * @apiParam {String} queryname 搜索内容
 * @apiParam {int} pagenum 当前页
 * @apiParam {int} pagesize 每页显示条数
 *
 * @apiSampleRequest api/front/songs
 *
 */

router.get("/songs", function (req, res) {
  var { pagenum, pagesize, queryname } = req.query;
  pagesize = Number(pagesize);
  pagenum = Number(pagenum - 1) * pagesize;
  var sql = "";
  var sqlparam = [];
  if (queryname === "" || queryname === undefined || queryname === NaN) {
    sql = `SELECT * FROM songs LIMIT ?,?`;
    sqlparam = [pagenum, pagesize];
  } else {
    sql = `SELECT * FROM songs WHERE songs.name LIKE ? LIMIT ?,?`;
    queryname = `%${queryname}%`;
    sqlparam = [queryname, pagenum, pagesize];
  }

  if (1 > 0) {
    db.query(sql, sqlparam, function (results, fields) {
      if (results.length) {
        db.query(`SELECT 	COUNT(*) as count  FROM 	songs`, [], function (r, f) {
          res.json({
            status: true,
            data: {
              total: r[0].count,
              songs: results,
            },
            msg: "获取成功",
          });
        });
      } else {
        res.json({
          status: false,
          msg: "获取失败",
        });
      }
    });
  } else {
    res.json({
      status: false,
      msg: "获取失败",
    });
  }
});

/**
 * @api {get} /api/front/songid 通过id搜索歌曲
 * @apiDescription 通过id搜索歌曲
 * @apiName songid
 * @apiGroup Front
 *
 * @apiParam {id} id 歌曲id
 *
 * @apiSampleRequest api/front/songid
 *
 */

router.get("/songid", function (req, res) {
  const { id } = req.query;
  // console.log(req.query);
  var sql = `
  SELECT
    *
  FROM
    songs
  WHERE
	songs.id = ?
  `;
  db.query(sql, [id], function (results, fields) {
    if (results.length) {
      res.json({
        status: true,
        data: results,
        msg: "获取成功",
      });
    } else {
      res.json({
        status: false,
        msg: "获取失败",
      });
    }
  });
});

module.exports = router;
